package com.projectbox.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.projectbox.dao.AnnouncementDao;
import com.projectbox.model.Announcement;
import com.projectbox.service.UserService;

@Controller
@RequestMapping("/admin/announcement")
public class ManageAnnouncementController {
	private Logger logger = LoggerFactory.getLogger(getClass()); 
	
	private AnnouncementDao announcementDao;
	private UserService userService;
	
	public void setAnnouncementDao(AnnouncementDao announcementDao) {
		this.announcementDao = announcementDao;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping(method = RequestMethod.GET)
    public String displayAnnounments(Model model) {
		model.addAttribute("announcements",
				this.announcementDao.getAnnouncements());
		
		logger.debug(userService.getCurrentUser().toString());
		
		model.addAttribute("user", userService.getCurrentUser());
		
		return "manageannouncement";
	}
	
	// response to 'manageannouncement.jsp'
	@RequestMapping(params = "newAnnouncement", method = RequestMethod.POST)
	public String newAnnouncement(Model model) {
		model.addAttribute("user", userService.getCurrentUser());
	
		return "newannouncement";
	}
		
	@RequestMapping(params = "editAnnouncement", method = RequestMethod.POST)
	public String editAnnouncement(@RequestParam(value = "id", required = true) long id, 
			Model model) {
		Announcement anmt = this.announcementDao.getAnnouncement(id);
		if (anmt == null) {
			return "redirect:/error/400.jsp";
		}
		
		model.addAttribute("user", userService.getCurrentUser());
		model.addAttribute("announcement", anmt);
		
		return "editannouncement";
	}
	
	@RequestMapping(params = "deleteAnnouncement", method = RequestMethod.POST)
	public String deleteAnnouncement(@RequestParam(value = "id", required = true) long id) {
		this.announcementDao.delete(id);
		
		return "redirect:/admin/announcement";
	}
	
	// response to 'newannouncement.jsp'
	@RequestMapping(params = "newAnnouncementSubmit", method = RequestMethod.POST)
	public String newAnnouncementSubmit(@RequestParam(value = "announcementTitle", required = true) String title, 
			@RequestParam(value = "announcementContent", required = true) String content) {
		Announcement announcement = new Announcement();
		announcement.setTitle(title);
		announcement.setContent(content);
		// set current milliseconds as createdTime and modifiedTime
		long time = System.currentTimeMillis();
		announcement.setTimeCreated(time);
		announcement.setTimeModified(time);
		announcement.setAdminId(userService.getCurrentUser().getId());
		
		this.announcementDao.insert(announcement);
		return "redirect:/admin/announcement";
	}
	
	@RequestMapping(params = "newAnnouncementReset", method = RequestMethod.POST)
	public String newAnnouncementReset(Model model) {
		model.addAttribute("user", userService.getCurrentUser());
		
		return "newannouncement";
	}
	
	@RequestMapping(params = "newAnnouncementCancel", method = RequestMethod.POST)
	public String cancelAnnouncementSubmit() {
		return "redirect:/admin/announcement";
	}
	
	// response to 'editannouncement.jsp'
	@RequestMapping(params = "editAnnouncementSave", method = RequestMethod.POST)
	public String editAnnouncementSave(@RequestParam(value = "id", required = true) long id,
			@RequestParam(value = "announcementTitle", required = true) String title, 
			@RequestParam(value = "announcementContent", required = true) String content) {
		Announcement announcement = this.announcementDao.getAnnouncement(id);
		
		announcement.setTitle(title);
		announcement.setContent(content);
		// set current milliseconds as modifiedTime
		long time = System.currentTimeMillis();
		announcement.setTimeModified(time);
		
		this.announcementDao.update(announcement);
		
		return "redirect:/admin/announcement";
	}
	
	@RequestMapping(params = "editAnnouncementDelete", method = RequestMethod.POST)
	public String editAnnouncementDelete(@RequestParam(value = "id", required = true) long id) {
		this.announcementDao.delete(id);
		
		return "redirect:/admin/announcement";
	}

	@RequestMapping(params = "editAnnouncementCancel", method = RequestMethod.POST)
	public String editAnnouncementCancel() {
		return "redirect:/admin/announcement";
	}
	
}
